package homework.service.iml;

import homework.entity.Goods;
import homework.entity.User;
import homework.mapper.GoodsMapper;
import homework.mapper.UserMapper;
import homework.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author :黄靓仔
 * @description: TODO
 * @data :2020/8/14 13:57
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public List<User> queryAll() {
        return userMapper.queryAll();
    }

    @Override
    public User register(String userName,String zhangHao,String miMa) { //注册新用户
        User user=new User();
        user.setUserName(userName);
        user.setZhangHao(zhangHao);
        user.setAdminist(0);
        user.setBalance(new BigDecimal(0));
        user.setMiMa(miMa);
        User oldUser = userMapper.queryByName(zhangHao);
        if (oldUser == null) {
            userMapper.register(user);
            //oldUser = user;
            return oldUser;
        }
        return oldUser;
    }

    @Override
    public String login( String zhangHao,String miMa) {  //登录

        User oldUser = userMapper.login(zhangHao, miMa);
        if(oldUser==null){
            return "登录失败,请检查账号或者密码是否正确";
        }else{
            return "恭喜登录成功";
        }
    }
    @Override
    public String buy( String goodsName, Integer number,String zhangHao) { //用户购买的商品的名字和数量,删去了thisUser
        Goods goods = goodsMapper.queryByName(goodsName);
        // System.out.println(goods.toString());
        //System.out.println(userName);
        User user = userMapper.queryByName(zhangHao);
        if (user == null) {
            System.out.println("user是空对象");
        }
        System.out.println(user.toString());
        BigDecimal price = goods.getPrice().multiply(new BigDecimal(number));
        System.out.println("price:" + price);
        int flag = user.getBalance().compareTo(price);
        System.out.println("flag:" + flag);
        //if(flag==-1){
        // return "账户余额不够";
        //}else {
        //if (flag != -1) {
        if (goods == null || goods.getFlag() == 0) {
            return "货品不存在";
        } else {
            if (flag == -1) {
                return "账号余额不足";
            } else {
                if (goods.getInventory() >= number) {
                    goodsMapper.changeGoodsNum(goodsName, goods.getInventory() - number);

                    userMapper.buy(user.getBalance().subtract(price), user.getZhangHao());    //根据账号扣取用户余额

                    return "购买成功";
                } else {
                        return "商品数量不足";
                }
            }
        }
        //else{
            //return "账户余额不够";
        //}

    }

    @Override
    public void add_balance( BigDecimal balance,User thisUser) {//用户对余额充值
        userMapper.addBalance(thisUser.getZhangHao(), balance.add(thisUser.getBalance()));
    }
    @Override
    public User isAdministrator(String zhangHao,String miMa) {//判断用户是否为管理员
       // System.out.println("1");
        User oldUser = userMapper.login(zhangHao, miMa);
       // System.out.println("2");
        if(oldUser==null){
            System.out.println("空指针");
        }
        //System.out.println("isAdministrator没问题");
        return oldUser;
    }
}
